const app = getApp()
import {parseTime} from '../../common/js/utils'
Page({
  data: {
    type:1,
    navId:'',
    isLoading:false,
    total:0,
    List:[],
    pageData:{
      pageNo:1,
      pageSize:10
    },
  },
  onLoad: function (options) {
    let that = this
    that.setData({
      type:options.typenew,
      navId:options.id
    })
    // that.setData({
    //   List:that.data.type!=2?that.data.cardList:that.data.journalismList
    // })
    tt.setNavigationBarTitle({
      title: that.filterName(that.data.type,app.globalData.listName)
    });
    this.getList()
  },
  onShow(){
    // this.setData({
    //   List:[]
    // })
    
  },
  onPullDownRefresh(){
    // 1. 重置关键数据
    this.setData({
      'pageData.pageNo': 1,
      total:0,
      isLoading: false,
      List:[]
    })
    // 2. 重新发起请求 并关闭下拉窗口
    this.getList()
    tt.stopPullDownRefresh()
  },
  onReachBottom() {
    let that = this
    if(that.data.isLoading)return
    let pageNo =that.data.pageData.pageNo
    this.setData({
      'pageData.pageNo': pageNo += 1 // 让页码值自增 +1
    })
    if(that.data.List.length<that.data.total){
      that.getList() // 重新获取列表数据
    }else{
         tt.showToast({
           title: '暂无更多数据！',
           icon:'none'
         })
    }
  },
  getList(){
    let that = this
    that.setData({
      isLoading:true
    })
    let data = that.data.navId+'/'+that.data.pageData.pageNo+'/'+that.data.pageData.pageSize
    app.getRequest('/applet/content/page', 'GET', data).then(res=>{
      if(res.code==200){
      res.data.contentPage.records.map(item=>{
        item.createTime = parseTime(item.createTime,'{y}-{m}-{d} {h}:{i}:{s}')
        item.coverUrl = app.globalData.serverUrl+item.coverUrl

      })
      let arr = that.data.List.concat(res.data.contentPage.records)
        this.setData({
          List:arr,
          isLoading:false,
          total:res.data.contentPage.total
        })
      }else{
        that.setData({
          isLoading:false
        })
      }
    })
  },
  filterName(type,data){
  return  data.filter(item=>{
      if(item.type==type){
        return item.name
      }
    })[0].name
  },
  filterNames(type,data){
  return  data.filter(item=>{
      if(item.type==type){
        return item.name
      }
    })[0].name.slice(0,2)
  },
  dateilHeld(e){
    let that = this
    tt.navigateTo({
      url: "/pages/details/details?type="+that.filterNames(that.data.type,app.globalData.listName)+"&id="+e.currentTarget.dataset.id,
    });
  }
})